<?php
define('SMARTY_SPL_AUTOLOAD', 1);
define("HTML2PDF_VERSION", "4.02");

include_once(dirname(__FILE__).DIRECTORY_SEPARATOR."html2pdf_v".HTML2PDF_VERSION.DIRECTORY_SEPARATOR."html2pdf.class.php");

Yii::import('application.vendors.*');
Yii::import('application.vendors.*');
require_once ('Smarty-3.1.7/Smarty.class.php');

spl_autoload_register('smartyAutoload', true, true);

class PdfGenerator {
	public $html2pdf;
	
	/**
	 * Create a new Html2Pdf instance to convert HTML files in PDF.
	 *
	 * @param string $orientation Page orientation (default to P(ortrait)).
	 * @param string $format Page format (default to A4).
	 * @param string $langue File language (default to fr).
	 * @param boolean $unicode Generates a unicode file or not (true by default).
	 * @param string $encoding Files encoding (default to UTF-8).
	 * @param array $marges Array of 4 integer defining document margins (no margin by default).
	 */
	public function __construct($orientation = 'P', $format = 'A4', $langue='fr', $unicode=true, $encoding='ISO-8859-1', $marges = array(0,0,0,0))
	{
		$this->html2pdf = new HTML2PDF($orientation, $format, $langue, $unicode, $encoding, $marges);
	}

	public function writeHtml($template, $context, $debugVue = false)
	{
		$smarty = new Smarty();
		$smarty->debugging = false;
		
		$smarty->caching = true;
		$smarty->cache_lifetime = 120;
		
		foreach ($context->keySet() as $key) {
			$smarty->assign($key,$context->get($key),true);
		}
		
		ob_start();
		$smarty->display($template);
		$xml = ob_get_clean();
		
		$this->html2pdf->writeHTML($xml, $debugVue);
	}
	
	public function output($filename, $dest = 'S') {
		$this->html2pdf->Output($filename, $dest);
	}
}